package com.fengye.algorithms.leecode;

/**
 * https://leetcode.cn/problems/find-peak-element/
 *
 * 寻找最大值
 *
 * @Author fengye
 * @Date 2023/4/21 6:11
 **/
public class FindPeakElement162 {
    public static void main(String[] args) {
        new FindPeakElement162().findPeakElement(new int[]{1,2,3,1});
    }

    public int findPeakElement(int[] nums) {
        int left = 0, right = nums.length - 1;
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] > nums[mid + 1]) {
                right = mid;
            } else {
                left = mid + 1;
            }
        }
        return left;
    }

    public int findPeakElement1(int[] nums) {
            if(nums == null) {
                return 0;
            }

            int l = nums.length;

            if(l == 1) {
                return 0;
            }

            if(l == 2) {
                if(nums[0] > nums[1]) {
                    return 0;
                } else {
                    return 1;
                }
            }

            for(int i=1;i<nums.length - 1;i++) {
                int a = nums[i-1];
                int b = nums[i];
                int c = nums[i+1];

                if(b > a && b > c) {
                    return i;
                }
            }

            if(nums[l-1] > nums[l-2]) {
                return l-1;
            }

            return 0;

    }
}
